/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_LCD_COLOR_H__
#define __PONY_LCD_COLOR_H__

/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_config.h>
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

/* RGB565 */
#define LCD_COLOR_RGB565_WHITE      0xFFFF
#define LCD_COLOR_RGB565_BLACK      0x0000
#define LCD_COLOR_RGB565_RED        0xF800
#define LCD_COLOR_RGB565_GREEN      0x07E0
#define LCD_COLOR_RGB565_BLUE       0x001F
#define LCD_COLOR_RGB565_CYAN       0x7FFF
#define LCD_COLOR_RGB565_MAGENTA    0xF81F
#define LCD_COLOR_RGB565_YELLOW     0xFFE0
#define LCD_COLOR_RGB565_GRAY       0x8430
#define LCD_COLOR_RGB565_LIGHTBLUE  0x7D7C
#define LCD_COLOR_RGB565_LIGHTGREEN 0x841F
#define LCD_COLOR_RGB565_DARKBLUE   0x01CF
#define LCD_COLOR_RGB565_BROWN      0xBC40

/* LCD colors (ARGB8888) */
#define LCD_COLOR_BLUE         LCD_COLOR(0xFF0000FFUL)
#define LCD_COLOR_GREEN        LCD_COLOR(0xFF00FF00UL)
#define LCD_COLOR_RED          LCD_COLOR(0xFFFF0000UL)
#define LCD_COLOR_CYAN         LCD_COLOR(0xFF00FFFFUL)
#define LCD_COLOR_MAGENTA      LCD_COLOR(0xFFFF00FFUL)
#define LCD_COLOR_YELLOW       LCD_COLOR(0xFFFFFF00UL)
#define LCD_COLOR_LIGHTBLUE    LCD_COLOR(0xFF8080FFUL)
#define LCD_COLOR_LIGHTGREEN   LCD_COLOR(0xFF80FF80UL)
#define LCD_COLOR_LIGHTRED     LCD_COLOR(0xFFFF8080UL)
#define LCD_COLOR_LIGHTCYAN    LCD_COLOR(0xFF80FFFFUL)
#define LCD_COLOR_LIGHTMAGENTA LCD_COLOR(0xFFFF80FFUL)
#define LCD_COLOR_LIGHTYELLOW  LCD_COLOR(0xFFFFFF80UL)
#define LCD_COLOR_DARKBLUE     LCD_COLOR(0xFF000080UL)
#define LCD_COLOR_DARKGREEN    LCD_COLOR(0xFF008000UL)
#define LCD_COLOR_DARKRED      LCD_COLOR(0xFF800000UL)
#define LCD_COLOR_DARKCYAN     LCD_COLOR(0xFF008080UL)
#define LCD_COLOR_DARKMAGENTA  LCD_COLOR(0xFF800080UL)
#define LCD_COLOR_DARKYELLOW   LCD_COLOR(0xFF808000UL)
#define LCD_COLOR_WHITE        LCD_COLOR(0xFFFFFFFFUL)
#define LCD_COLOR_LIGHTGRAY    LCD_COLOR(0xFFD3D3D3UL)
#define LCD_COLOR_GRAY         LCD_COLOR(0xFF808080UL)
#define LCD_COLOR_DARKGRAY     LCD_COLOR(0xFF404040UL)
#define LCD_COLOR_BLACK        LCD_COLOR(0xFF000000UL)
#define LCD_COLOR_BROWN        LCD_COLOR(0xFFA52A2AUL)
#define LCD_COLOR_ORANGE       LCD_COLOR(0xFFFFA500UL)

/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/

#define LCD_COLOR_ARGB8888_2_RGB565(color)                                                         \
    ((((color & 0xFFU) >> 3) & 0x1FU) | (((((color & 0xFF00U) >> 8) >> 2) & 0x3FU) << 5) |         \
     (((((color & 0xFF0000U) >> 16) >> 3) & 0x1FU) << 11))

#define LCD_COLOR_RGB565_2_ARGB8888(color)                                                         \
    (((((((color >> 11) & 0x1FU) * 527) + 23) >> 6) << 16) |                                       \
     ((((((color >> 5) & 0x3FU) * 259) + 33) >> 6) << 8) | ((((color & 0x1FU) * 527) + 23) >> 6) | \
     0xFF000000)

// color is ARGB8888 or RGB888
#if defined(CONFIG_PIXEL_FORMAT_RGB565)
#define LCD_COLOR(color) LCD_COLOR_ARGB8888_2_RGB565(color)
#else
#define LCD_COLOR(color) (color)
#endif

/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_LCD_COLOR_H__ */
